package com.yutian.example.demo.common.utils;

import com.google.common.base.CaseFormat;
import com.google.common.collect.Maps;
import org.apache.commons.collections.MapUtils;

import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {
    /**
     * 驼峰转下划线
     * @param str
     * @return
     */
    public static String toLowerUnderScore(String str) {
        return CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, str);
    }

    /**
     * 下划线转驼峰
     * @param str
     * @return
     */
    public static String toLowerCamel(String str) {
        return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, str);
    }

    public static boolean hasLength(String str) {
        return (str != null && str.length() > 0);
    }

    public static boolean hasText(String str) {
        if (!hasLength(str)) {
            return false;
        }
        int strLen = str.length();
        for (int i = 0; i < strLen; i++) {
            if (!Character.isWhitespace(str.charAt(i))) {
                return true;
            }
        }
        return false;
    }
    /**
     * 转字符串
     * @author zhoulei
     * @date 2017年8月30日 上午10:15:23
     * @param obj
     * @return
     */
    public static String toStr(Object obj) {
        return toStr(obj, "");
    }
    
    /**
     * 转字符串
     * @author zhoulei
     * @date 2017年8月30日 上午10:15:18
     * @param obj
     * @param defaultValue
     * @return
     */
    public static String toStr(Object obj, String defaultValue) {
        if(obj == null) {
            return defaultValue;
        }
        if (obj instanceof String) {
            return (String) obj;
        }
        return obj.toString();
    }

    /**
     * 去除strName中的中文
     * 
     * @param strName
     * @return
     */
    public static String replaceChinese(String strName) {
        if (null == strName) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        char[] ch = strName.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            char c = ch[i];
            if (!StringUtil.isChinese(c)) {
                sb.append(c);
            }
        }
        return sb.toString().trim();
    }

    // 根据Unicode编码完美的判断中文汉字和符号
    public static boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {
            return true;
        }
        return false;
    }

    // 完整的判断中文汉字和符号
    public static boolean isChinese(String strName) {
        char[] ch = strName.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            char c = ch[i];
            if (isChinese(c)) {
                return true;
            }
        }
        return false;
    }

    
    /** 
     * 判断字符串是否是乱码 
     * 
     * @param strName 字符串 
     * @return 是否是乱码 
     */  
    private static final Pattern NOTMESSYCODE = Pattern.compile("\\s*|\t*|\r*|\n*");
    public static boolean isMessyCode(String strName) {  
        Matcher m = NOTMESSYCODE.matcher(strName);  
        String after = m.replaceAll("");  
        String temp = after.replaceAll("\\p{P}", "");
        char[] ch = temp.trim().toCharArray();  
        float chLength = ch.length;  
        float count = 0;  
        for (int i = 0; i < ch.length; i++) {  
            char c = ch[i];  
            if (!Character.isLetterOrDigit(c)) {  
                if (!isChinese(c)) {  
                    count = count + 1;  
                }  
            }  
        }  
        float result = count / chLength;  
        return result > 0.4;
    } 

    public static Map<String, String> toStrMap(Map<String, Object> map) {
        Map<String, String> res = Maps.newHashMap();
        if (MapUtils.isEmpty(map)) {
            return res;
        }
        for(Entry<String, Object> entry : map.entrySet()){
            res.put(entry.getKey(), toStr(entry.getValue()));
        }
        return res;
    }
    

}
